$(document).ready(function(){
    // $(".sendcode").hide();
    // $(".recode").show();

    $(".recode").hide();
    $(".sendcode").show();



    //后端返回的验证码
    var compare_code;
    
    var user_name;

    //点击发送验证码按钮获取验证码
    $("#verifi-btn").click(function(){

        var timer="";
        var nums=60;
        var validCode=true;//定义该变量是为了处理后面的重复点击事件
        
        var code=$('.code');
        if(validCode){
            code.text("发送中...");
            code.css("color", "rgb(170, 170, 170)");
            validCode=false;
            timer=setInterval(function(){
                if(nums>1){
                    nums--;
                    code.text(nums+"秒后重新发送");
                    code.css("color", "rgb(170, 170, 170)");
                }
                else{
                    clearInterval(timer);
                    nums=60;//重置回去
                    validCode=true;
                    code.css("color", "#e4393c");
                    code.text("发送验证码");
                }
            },1000)
        }
       

        var user_email;
        var reg = new RegExp("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$");


        
        if($("#user-name").val()==""){
            alert("用户名不能为空！")
            return;
        }
        else{
            user_name=$("#user-name").val();
        }

        if($("#user-email").val()==""){
            alert("邮箱不能为空！")
            return;
        }
        else if($("#user-email").val()!==""){
            
            if(!reg.test($("#user-email").val())){
                alert("邮箱格式不正确，请重新输入。");
                return;
            }
            else{
                user_email=$("#user-email").val();
                
            }
        }

        var data1={
            name:user_name,
        }
        $.ajax({
            type: 'GET',
            url: "http://8.130.103.251:8080/user/getuser", 
            data: data1,
            ContentType: 'application/json;charset=utf-8',
            
            success: function(data) {   
              if(data.data==null){
                alert("用户民不存在！");
                return
              }
              else{
                if(data.data.email!==user_email){
                    alert("用户名与邮箱不匹配");
                    return
                }
                else{
                    var data={
                        email:user_email,
                    }
                    $.ajax({
                        type: 'GET',
                        url: "http://8.130.103.251:8080/getCheckCode", 
                        data: data,
                        ContentType: 'application/json;charset=utf-8',
                        
                        success: function(data) {   
                           compare_code=data;
                        },
                        error: function(xhr, type) {
                            console.log("Error");
                        }
                     });
                }
              }
              
            },
            error: function(xhr, type) {
                console.log("Error");
            }
         });


       
    
    });


    //点击下一步
    $(".btn-next").click(function () {

        var user_email;
        var verifi_code;


        var reg = new RegExp("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$");

        if($("#user-name").val()==""){
            alert("用户名不能为空！")
            return;
        }
        else{
            user_name=$("#user-name").val();
        }

        

        if($("#user-email").val()==""){
            alert("邮箱不能为空！")
            return;
        }
        else if($("#user-email").val()!==""){
            
            if(!reg.test($("#user-email").val())){
                alert("邮箱格式不正确，请重新输入。");
                return;
            }
            else{
                user_email=$("#user-email").val();
                
            }
        }


        if($("#verifi-code").val()==""){
            alert("验证码不能为空！")
            return;
        }
        else{
            verifi_code=$("#verifi-code").val();
        }

        if(!(verifi_code==compare_code)){
            alert("验证码错误！");
            return
        }
        else
        {
            $(".sendcode").hide();
            $(".recode").show();
        }
    });

    $("#submit-btn").click(function(){
        var new_pwd=$("#new-pwd").val();
        var confirm_pwd=$("#confirm-pwd").val();

        if(new_pwd!==confirm_pwd){
            alert("两次输入密码不一致！请重新输入。");
            return
        }
        else{
            var data={
                username:user_name,
                password:confirm_pwd,
            }

            $.ajax({
                type: 'POST',
                url: "http://8.130.103.251:8080/user/changeuserpd", 
                data: data,
                ContentType: 'application/json;charset=utf-8',
                
                success: function(data) {   
                   alert(data.msg);
                   window.location.href = "login.html";
                },
                error: function(xhr, type) {
                    console.log("Error");
                }
             });
        }
    })


});